import random
def generate_question():
    operator = ['+', '-', '*', '/']
    op = random.choice(operator)

    if op == '+':
        a = random.randint(1, 100)
        b = random.randint(1, 100)
        answer = a + b
    elif op == '-':
        a = random.randint(1, 100)
        b = random.randint(1, 100)
        a, b = max(a, b), min(a, b)
        answer = a - b
    elif op == '*':
        a = random.randint(1, 20)
        b = random.randint(1, 10)
        answer = a * b
    else:
        b = random.randint(1, 10)
        a = b * random.randint(1, 10)
        answer = a // b

    return str(a) + " " + op + " " + str(b) + " = ", answer


def math_quiz():
    print("=" * 50)
    print("数学测试")
    print("回答10道四则运算题")
    print("=" * 50)

    correct = 0
    for i in range(1, 11):
        question, answer = generate_question()
        while True:
            try:
                user_answer = int(input("第" + str(i) + "题: " + question))
                if user_answer == answer:
                    print("RIGHT！\n")
                    correct += 1
                else:
                    print("WRONG,Right answer is: " + str(answer) + "\n")
                break
            except ValueError:
                print("请输入一个整数！")

    score = correct * 10
    print("\n测验结束！")
    print("你的得分: " + str(score) + "分")
    print("正确率: " + str(correct) + "/10 (" + str(score) + "%)")

    if score == 100:
        print("Good")
    elif score >= 70:
        print("OK")
    else:
        print("NO")


if __name__ == "__main__":
    while True:
        math_quiz()
        again = input("\n是否再来一次？(y/n): ").lower()
        if again != 'y':
            print("Bye")
            break
27
